package com.pboc.service;

import com.pboc.base.BaseException;
import com.pboc.base.PageModel;
import com.pboc.dao.SysPersonDao;
import com.pboc.entity.SysPerson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SysPersonService {
    @Autowired
    private SysPersonDao sysPersonDao;
    public void getList(PageModel<SysPerson> pageModel){
        try {
            pageModel.setRows(sysPersonDao.pageQuery(pageModel));
            pageModel.setTotal(sysPersonDao.getListCount(pageModel.getQueryObj()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void deletePerson(Integer id){
        sysPersonDao.delete(id);
    }
    public void insertPerson(SysPerson sysPerson) throws BaseException {
        SysPerson parm=new SysPerson();
        parm.setUserNum(sysPerson.getUserNum());
        List<SysPerson> sysPeople=sysPersonDao.getList(parm);
        if(CollectionUtils.isEmpty(sysPeople)){
            sysPersonDao.save(sysPerson);

        }else {
            throw new BaseException("编号重复");
        }
    }
    public void updatePerson(SysPerson sysPerson) throws BaseException {
        SysPerson parm=new SysPerson();
        parm.setUserNum(sysPerson.getUserNum());
        List<SysPerson> sysPeople=sysPersonDao.getList(parm);
        parm=sysPersonDao.getById(sysPerson.getUserId());
        if(CollectionUtils.isEmpty(sysPeople) || parm.getUserNum().equals(sysPerson.getUserNum())){
            sysPersonDao.update(sysPerson);
        }else {
            throw new BaseException("编号重复");
        }
    }

    public List<SysPerson> getPersonByDept(Integer deptId){
        SysPerson person=new SysPerson();
        person.setDeptId(deptId);
        person.setIsDelete(0);
        return sysPersonDao.getList(person);
    }
    public List<SysPerson> getCarPayPrintList() {
        return sysPersonDao.getCarPayPrintList();
    }
    public SysPerson getById(Integer userId){
        return sysPersonDao.getById(userId);
    }

}
